package com.rambo.baselib;

import com.rambo.baselib.bean.ToolbarHolder;
import com.rambo.baselib.msgbus.EventBus;
import com.rambo.baselib.mvp.IView;
import com.rambo.baselib.util.Loading;
import com.rambo.baselib.util.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.event.commonevent.MatchingSkills;

import java.io.Serializable;

public abstract class BaseAbilitySlice extends AbilitySlice implements IView {
    protected String TAG = getClass().getSimpleName();
    protected Loading loading;
    protected ToolbarHolder toolbarHolder = new ToolbarHolder();
    private MatchingSkills matchingSkills = new MatchingSkills();
    private CommonEventSubscriber commonEventSubscriber;

    @Override
    protected void onStart(Intent intent) {
        //状态栏颜色
        getWindow().setStatusBarColor(Color.getIntColor("#177EE6"));
        getWindow().setStatusBarVisibility(Component.VISIBLE);
        super.onStart(intent);
        ComponentContainer rootComponent = (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_slice_base, null, false);
        StackLayout toolbarContainer = (StackLayout) rootComponent.findComponentById(ResourceTable.Id_toolbar_container);
        if (isToolbarVisible() && getToolbarLayoutResId() > 0) {
            toolbarContainer.setVisibility(Component.VISIBLE);
            Component component = LayoutScatter.getInstance(this).parse(getToolbarLayoutResId(), null, false);
            toolbarContainer.addComponent(component, new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT,
                    StackLayout.LayoutConfig.MATCH_PARENT));
            toolbarHolder.bindToolbarHolder(component);
            toolbarHolder.setClickedListener(ResourceTable.Id_toolbar_iv_back, new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    onBackPressed();
                }
            });
        }
        StackLayout contentContainer = (StackLayout) rootComponent.findComponentById(ResourceTable.Id_content_container);
        Component contentComponent = LayoutScatter.getInstance(this).parse(getLayoutResId(), null, false);
        contentContainer.addComponent(contentComponent, new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT,
                StackLayout.LayoutConfig.MATCH_PARENT));
        loading = new Loading(this);
        loading.init(contentComponent);
        loading.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                reloadData();
            }
        });
        contentContainer.addComponent(loading, new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_PARENT));


        int count = contentContainer.getChildCount();
        for (int i = 0; i < count; i++) {
            Component c = contentContainer.getComponentAt(i);
            LogUtil.e("===" + c.getClass().getSimpleName() + " visible=" + c.getVisibility());
        }
        subscriberEvent();
        setUIContent(rootComponent);
//        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        getParamsByIntent(intent);
        initView();
        registerClick();
        fillData();
    }
    private void subscriberEvent() {
        matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(EventBus.EVENT_ACTION);
        commonEventSubscriber = new CommonEventSubscriber(new CommonEventSubscribeInfo(matchingSkills)) {
            @Override
            public void onReceiveEvent(CommonEventData commonEventData) {
                LogUtil.e("======onReceiveEvent====" + Thread.currentThread().getName());
                int type = commonEventData.getIntent().getIntParam(EventBus.TYPE_KEY, -1);
                Serializable serializable = commonEventData.getIntent().getSerializableParam(EventBus.DATA_KEY);
                onEventReceive(type, serializable);
            }
        };
        EventBus.getEventBus().subscriber(commonEventSubscriber);
    }

    /**
     * 订阅接收的信息
     *
     * @param eventBean
     */
    protected void onEventReceive(int type, Object eventBean) {
        LogUtil.e("======onEventReceive====" + eventBean!=null?eventBean.toString():"");
    }

    protected void getParamsByIntent(Intent intent) {

    }

    /**
     * 重新加载数据
     */
    protected void reloadData() {

    }

    /**
     * 填充数据
     */
    protected void fillData() {
    }

    /**
     * 注册点击事件
     */
    protected void registerClick() {

    }


    /**
     * 初始化视图
     */
    protected void initView() {

    }

    protected abstract int getLayoutResId();

    /**
     * toolbar 的布局
     *
     * @return
     */
    protected int getToolbarLayoutResId() {
        return ResourceTable.Layout_componet_toolbar;
    }

    /**
     * 是否启用toolbar
     *
     * @return
     */
    protected boolean isToolbarVisible() {
        return true;
    }


    @Override
    public void showLoading() {
        showLoading(null, false);
    }

    @Override
    public void showLoading(String msg, boolean isDialogStyle) {
        if (isDialogStyle) {
            return;
        }
        loading.updateLoadState(Loading.LoadingState.LOADING);
    }

    @Override
    public void hideLoading() {
        loading.updateLoadState(Loading.LoadingState.SUCCESS);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getEventBus().unSubscriber(commonEventSubscriber);
    }
}
